package com.tms.springboot.controller;


import com.github.pagehelper.PageInfo;
import com.tms.springboot.aggregation.pojo.PdCoreOrg;
import com.tms.springboot.base.pojo.PdAgencyScope;
import com.tms.springboot.feign.PdAgencyScopeFeign;
import com.tms.springboot.result.JsonResult;
import com.tms.springboot.result.ResultTool;
import com.tms.springboot.service.PdCoreOrgService;
import com.tms.springboot.vo.CoreOrgJobInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.tms.springboot.utils.UUId;
import com.tms.springboot.vo.PdCoreOrgVo;

import java.beans.Transient;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 组织 前端控制器
 * </p>
 *
 * @author cjj
 * @since 2023-06-14
 */
@RestController
@RequestMapping("/pd-core-org")
public class PdCoreOrgController {


    @Autowired
    private PdCoreOrgService coreOrgService;

    //机构作业范围feign调用
    @Autowired
    private PdAgencyScopeFeign pdAgencyScopeFeign;



    /**
     * todo 查询所有的组织机构
     * operator hui
     *
     * @return JsonResult
     */
    @GetMapping("/queryAll")
    public JsonResult queryAll() {
        List<PdCoreOrg> coreOrgList = coreOrgService.list();
        return coreOrgList != null ? ResultTool.success(coreOrgList) : ResultTool.fail();
    }

    /**
     * todo 根据id的组织机构
     * operator hui
     *
     * @return JsonResult
     */
    @GetMapping("/queryById/{coreOrgId}")
    public JsonResult queryById(@PathVariable String coreOrgId) {
        PdCoreOrg coreOrg = coreOrgService.getById(coreOrgId);
        return coreOrg != null ? ResultTool.success(coreOrg) : ResultTool.fail();
    }

    @Autowired
    private PdCoreOrgService pdCoreOrgService;

    /**
     * 查询所有机构
     *
     * @return
     */
    @RequestMapping(value = "/queryCoreOrgListAll")
    public JsonResult queryCoreOrgListAll() {
        List<PdCoreOrg> list = pdCoreOrgService.list();
        return list != null ? ResultTool.success(list) : ResultTool.fail();
    }

    /**
     * 查询所有机构树状
     *
     * @return
     */
    @RequestMapping("/queryCoreOrgList")
    public JsonResult queryCoreOrgList() {
        List<PdCoreOrgVo> pdCoreOrgVos = pdCoreOrgService.queryCoreOrgList();

        return pdCoreOrgVos != null ? ResultTool.success(pdCoreOrgVos) : ResultTool.fail();
    }

    /**
     * 添加组织
     *
     * @param pdCoreOrg
     * @return
     */

    @Transient
    @RequestMapping(value = "/AddCoreOrg")
    public JsonResult addCoreOrg(@RequestBody PdCoreOrg pdCoreOrg) {
        pdCoreOrg.setId(UUId.nextId());
        if (pdCoreOrg.getOrgType() != 1) {
            PdAgencyScope pd = new PdAgencyScope();
            pd.setId(UUId.nextId());
            pd.setAgencyId(pdCoreOrg.getId());
            switch (pdCoreOrg.getOrgType()) {
                case 2:
                    pd.setAreaId(pdCoreOrg.getProvinceId().toString());
                    break;
                case 3:
                    pd.setAreaId(pdCoreOrg.getCityId().toString());
                    break;
                case 4:
                    pd.setAreaId(pdCoreOrg.getCountyId().toString());
                    break;
            }
            this.pdAgencyScopeFeign.addAgencyScope(pd);
        }
        pdCoreOrg.setBusinessHours("周一到周日 8点-20点");
        return this.pdCoreOrgService.save(pdCoreOrg) ? ResultTool.success() : ResultTool.fail();
    }


    @RequestMapping("/updateCoreOrg")
    public JsonResult updateCoreOrg(@RequestBody PdCoreOrg pdCoreOrg) {

        if (pdCoreOrg.getOrgType() != 1) {
            PdAgencyScope pd = new PdAgencyScope();
            pd.setId(UUId.nextId());
            pd.setAgencyId(pdCoreOrg.getId());
            switch (pdCoreOrg.getOrgType()) {
                case 2:
                    pd.setAreaId(pdCoreOrg.getProvinceId().toString());
                    break;
                case 3:
                    pd.setAreaId(pdCoreOrg.getCityId().toString());
                    break;
                case 4:
                    pd.setAreaId(pdCoreOrg.getCountyId().toString());
                    break;
            }
            this.pdAgencyScopeFeign.updateAgencyScope(pd);
        }
        return this.pdCoreOrgService.updateById(pdCoreOrg) ? ResultTool.success() : ResultTool.fail();
    }


    @PostMapping("/queryCoreOrgList")
        public PageInfo<CoreOrgJobInfoVo> queryCoreOrgList(@RequestBody Map<Object,Object> map){
        return this.pdCoreOrgService.queryCoreOrgListByFuzzyQueries(map);
    }

}
